﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media;
using Microsoft.Phone.Maps.Controls;
using System.Device.Location;
using System.ComponentModel;
using System.Diagnostics;

namespace INdTFramework.Controls
{
    public partial class PinOnMap : UserControl
    {
        bool shownMap;
        MapLayer layer;
        MapOverlay overlay;

        public static DependencyProperty PinProperty = DependencyProperty.Register("Pin", typeof(ImageSource), typeof(PinOnMap), new PropertyMetadata(PinChanged));
        public static DependencyProperty CoordinatesProperty = DependencyProperty.Register("Coordinates", typeof(GeoCoordinate), typeof(PinOnMap), new PropertyMetadata(CoordinatesChanged));

        public ImageSource Pin
        {
            get
            {
                return GetValue(PinProperty) as ImageSource;
            }

            set
            {
                SetValue(PinProperty, value);
                CheckCoordinates();
            }
        }

        [TypeConverter(typeof(GeoCoordinateConverter))]
        public GeoCoordinate Coordinates
        {
            get
            {
                return GetValue(CoordinatesProperty) as GeoCoordinate;
            }

            set
            {
                SetValue(CoordinatesProperty, value);
                CheckPin();
            }
        }

        static void CoordinatesChanged(DependencyObject dependecyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            var control = dependecyObject as PinOnMap;

            if (control != null)
            {
                control.Coordinates = dependencyPropertyChangedEventArgs.NewValue as GeoCoordinate;
            }
        }

        static void PinChanged(DependencyObject dependecyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            var control = dependecyObject as PinOnMap;

            if (control != null)
            {
                control.Pin = dependencyPropertyChangedEventArgs.NewValue as ImageSource;
            }
        }

        public PinOnMap()
        {
            InitializeComponent();

            layer = new MapLayer();
            overlay = new MapOverlay();
            overlay.PositionOrigin = new Point(0.5, 0.5);
            shownMap = false;
        }

        void CheckPin()
        {
            if (Pin != null && !shownMap)
            {
                ShowMap();
            }
        }

        void CheckCoordinates()
        {
            if (Coordinates != null && !shownMap)
            {
                ShowMap();
            }
        }

        void ShowMap()
        {
            shownMap = true;
            overlay.Content = new Image { Source = Pin };
            overlay.GeoCoordinate = Coordinates;

            layer.Clear();
            layer.Add(overlay);

            Map.Layers.Clear();
            Map.Layers.Add(layer);
            Map.Center = Coordinates;
        }
    }
}
